home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
os2tools
/
bnklysrc
/
sched.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-02-25
|
18KB
|
586 lines
/*--------------------------------------------------------------------------*/
/* */
/* */
/* ------------ Bit-Bucket Software <no-Inc> */
/* \ 10001101 / Writers and Distributors of */
/* \ 011110 / No-Cost<no-tm> Software. */
/* \ 1011 / */
/* ------ */
/* */
/* Copyright (C) 1987, 1988, 1989 by Robert Hartman and Vincent Perriello */
/* */
/* */
/* This module was written by Bob Hartman */
/* */
/* */
/* BinkleyTerm Scheduler Routines */
/* */
/* */
/* For complete details of the licensing restrictions, please refer */
/* to the License agreement, which is published in its entirety in */
/* the MAKEFILE and BT.C, and also contained in the file LICENSE.210. */
/* */
/* USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE */
/* BINKLEYTERM LICENSING AGREEMENT. IF YOU DO NOT FIND THE TEXT OF */
/* THIS AGREEMENT IN ANY OF THE AFOREMENTIONED FILES, OR IF YOU DO */
/* NOT HAVE THESE FILES, YOU SHOULD IMMEDIATELY CONTACT THE AUTHORS */
/* AT THE ADDRESSES LISTED BELOW. IN NO EVENT SHOULD YOU PROCEED TO */
/* USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE */
/* BINKLEYTERM LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU */
/* ARE ABLE TO REACH WITH THE AUTHORS. */
/* */
/* */
/* The Authors can be reached at the following addresses: */
/* */
/* Robert C. Hartman Vincent E. Perriello */
/* Spark Software VEP Software */
/* 427-3 Amherst Street 111 Carroll Street */
/* CS2032, Suite 232 Naugatuck, CT 06770 */
/* Nashua, NH 03061 */
/* */
/* FidoNet 1:132/101 FidoNet 1:141/491 */
/* Data (603) 888-8179 Data (203) 729-7569 */
/* */
/* Please feel free to contact us at any time to share your comments */
/* about our software and/or licensing policies. */
/* */
/*--------------------------------------------------------------------------*/
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
#include <time.h>
#ifdef __TURBOC__
#include "tc_utime.h"
#include <alloc.h>
#include <mem.h>
#else
#include <sys/utime.h>
#include <malloc.h>
#include <memory.h>
#endif
#include "com.h"
#include "xfer.h"
#include "zmodem.h"
#include "keybd.h"
#include "sbuf.h"
#include "sched.h"
#include "externs.h"
#include "prototyp.h"
static void write_stats (void);
/**************************************************************************/
/*** This MUST be exactly 16 total bytes including the terminating null ***/
/*** or the routines read_sched() and write_sched() must be changed!!!! ***/
/**************************************************************************/
static char *BinkSched = "BinkSchedule 03"; /* Version of scheduler */
void find_event ()
{
int cur_day;
int cur_hour;
int cur_minute;
int cur_mday;
int cur_mon;
int cur_year;
int junk;
int our_time;
int i;
char cmds[150];
/* Get the current time in minutes */
dostime (&cur_hour, &cur_minute, &junk, &junk);
our_time = (cur_hour % 24) * 60 + (cur_minute % 60);
/* Get the current day of the week */
dosdate (&cur_mon, &cur_mday, &cur_year, &cur_day);
cur_day = 1 << cur_day;
cur_event = -1;
if (cur_mday != hist.which_day)
{
write_stats ();
memset (&hist, 0, sizeof (HISTORY));
hist.which_day = cur_mday;
if (un_attended && fullscreen)
{
do_today ();
sb_show ();
}
}
/* Go through the events from top to bottom */
for (i = 0; i < num_events; i++)
{
if (our_time >= e_ptrs[i]->minute)
{
if ((cur_day & e_ptrs[i]->days) &&
((!e_ptrs[i]->day) || (e_ptrs[i]->day == (char)cur_mday)) &&
((!e_ptrs[i]->month) || (e_ptrs[i]->month == (char)cur_mon)))
{
if (((our_time - e_ptrs[i]->minute) < e_ptrs[i]->length) ||
((our_time == e_ptrs[i]->minute) && (e_ptrs[i]->length == 0)) ||
((e_ptrs[i]->behavior & MAT_FORCED) && (e_ptrs[i]->last_ran != cur_mday)))
{
/* Are we not supposed to force old events */
if (((our_time - e_ptrs[i]->minute) > e_ptrs[i]->length) && (noforce))
{
e_ptrs[i]->last_ran = cur_mday;
continue;
}
if (e_ptrs[i]->last_ran != cur_mday)
{
screen_blank = 0;
set_xy ("");
status_line (":Starting Event %d", i + 1);
more_mail = 1;
/* Mark that this one is running */
e_ptrs[i]->last_ran = cur_mday;
/*
* Mark that we have not yet skipped it. After all, it just
* started!
*/
e_ptrs[i]->behavior &= ~MAT_SKIP;
/* Write out the schedule */
write_sched ();
/* If we are supposed to exit, then do it */
if (e_ptrs[i]->errlevel[0])
{
status_line ("#Exit at start of event with errorlevel %d",
e_ptrs[i]->errlevel[0]);
errl_exit (e_ptrs[i]->errlevel[0]);
}
else if (packer != NULL)
{
status_line ("#Running clean/pack sequence");
mdm_init (modem_busy);
DTR_OFF ();
scr_printf ("\033[H\033[2J");
vfossil_cursor (1);
if (cleanup != NULL)
{
strcpy (cmds, cleanup);
if (i >= 0)
strcat (cmds, e_ptrs[i]->cmd);
b_spawn (cmds);
}
strcpy (cmds, packer);
if (i >= 0)
strcat (cmds, e_ptrs[i]->cmd);
b_spawn (cmds);
if (fullscreen)
{
scr_printf ("\033[H\033[2J");
sb_dirty ();
opening_banner ();
mailer_banner ();
}
DTR_ON ();
mdm_init (modem_init);
status_line ("#System re-enabled after clean/pack sequence");
xmit_reset ();
}
cur_event = i;
xmit_reset ();
}
else
{
/* Don't do events that have been exited already */
if (e_ptrs[i]->behavior & MAT_SKIP)
continue;
}
cur_event = i;
if (e_ptrs[i]->behavior & MAT_NOREQ)
{
matrix_mask &= ~TAKE_REQ;
no_requests = 1;
}
else
{
matrix_mask |= TAKE_REQ;
no_requests = 0;
}
if (e_ptrs[i]->behavior & MAT_NOOUTREQ)
{
requests_ok = 0;
}
else
{
requests_ok = 1;
}
max_connects = e_ptrs[i]->with_connect;
max_noconnects = e_ptrs[i]->no_connect;
break;
}
}
}
}
}
int what_event ()
{
int cur_day;
int cur_hour;
int cur_minute;
int cur_mday;
int cur_mon;
int cur_year;
int junk;
int our_time;
int w_event;
int i;
/* Get the current time in minutes */
dostime (&cur_hour, &cur_minute, &junk, &junk);
our_time = cur_hour * 60 + cur_minute;
/* Get the current day of the week */
dosdate (&cur_mon, &cur_mday, &cur_year, &cur_day);
cur_day = 1 << cur_day;
w_event = -1;
/* Go through the events from top to bottom */
for (i = 0; i < num_events; i++)
{
if (our_time >= e_ptrs[i]->minute)
{
if ((cur_day & e_ptrs[i]->days) &&
((!e_ptrs[i]->day) || (e_ptrs[i]->day == (char)cur_mday)) &&
((!e_ptrs[i]->month) || (e_ptrs[i]->month == (char)cur_mon)))
{
if (((our_time - e_ptrs[i]->minute) < e_ptrs[i]->length) ||
((our_time == e_ptrs[i]->minute) && (e_ptrs[i]->length == 0)) ||
((e_ptrs[i]->behavior & MAT_FORCED) && (e_ptrs[i]->last_ran != cur_mday)))
{
/* Are we not supposed to force old events */
if (((our_time - e_ptrs[i]->minute) > e_ptrs[i]->length) && (noforce))
{
e_ptrs[i]->last_ran = cur_mday;
continue;
}
if ((e_ptrs[i]->last_ran == cur_mday) &&
(e_ptrs[i]->behavior & MAT_SKIP))
{
/* Don't do events that have been exited already */
continue;
}
w_event = i;
break;
}
}
}
}
return (w_event);
}
void read_sched ()
{
char temp1[80], temp2[80];
EVENT *sptr;
struct stat buffer1, buffer2;
FILE *f;
int i;
strcpy (temp1, BINKpath);
strcpy (temp2, BINKpath);
strcat (temp1, "Binkley.Scd");
strcat (temp2, "Binkley.Evt");
if (stat (temp1, &buffer1))
{
return;
}
if (stat (temp2, &buffer2))
{
strcpy (temp2, BINKpath);
strcat (temp2, config_name);
if (stat (temp2, &buffer2))
{
return;
}
}
if ((buffer1.st_atime < buffer2.st_atime) ||
(buffer1.st_size < sizeof (EVENT)))
{
return;
}
if ((sptr = (EVENT *) malloc ((int) buffer1.st_size - 16 - sizeof (HISTORY))) == NULL)
{
return;
}
if ((f = fopen (temp1, "rb")) == NULL)
{
return;
}
temp1[0] = '\0';
fread (temp1, 16, 1, f);
if (strcmp (temp1, BinkSched) != 0)
{
fclose (f);
return;
}
fread (&hist, (int) sizeof (HISTORY), 1, f);
fread (sptr, (int) buffer1.st_size - 16 - sizeof (HISTORY), 1, f);
got_sched = 1;
num_events = (int) ((buffer1.st_size - 16 - sizeof (HISTORY)) / sizeof (EVENT));
for (i = 0; i < num_events; i++)
{
e_ptrs[i] = sptr++;
}
fclose (f);
return;
}
void write_sched ()
{
char temp1[80], temp2[80];
FILE *f;
int i;
struct stat buffer1;
struct utimbuf times;
long t;
/* Get the current time */
t = time (NULL);
strcpy (temp1, BINKpath);
strcpy (temp2, BINKpath);
strcat (temp1, "Binkley.Scd");
strcat (temp2, "Binkley.Evt");
/* Get the current stat for .Evt file */
if (!stat (temp2, &buffer1))
{
/*
* If it is newer than current time, we have a problem and we must
* reset the file date - yucky, but it will probably work
*/
if (t < buffer1.st_atime)
{
times.actime = buffer1.st_atime;
times.modtime = buffer1.st_atime;
status_line ("!Date rollover problem?");
}
else
{
times.actime = t;
times.modtime = t;
}
}
if ((f = fopen (temp1, "wb")) == NULL)
{
return;
}
fwrite (BinkSched, 16, 1, f);
fwrite (&hist, (int) sizeof (HISTORY), 1, f);
for (i = 0; i < num_events; i++)
{
/* If it is skipped, but not dynamic, reset it */
if ((e_ptrs[i]->behavior & MAT_SKIP) &&
(!(e_ptrs[i]->behavior & MAT_DYNAM)))
{
e_ptrs[i]->behavior &= ~MAT_SKIP;
}
/* Write this one out */
fwrite (e_ptrs[i], sizeof (EVENT), 1, f);
}
fclose (f);
utime (temp1, ×);
return;
}
static void write_stats ()
{
char temp1[80];
FILE *f;
strcpy (temp1, BINKpath);
strcat (temp1, "Binkley.Day");
if ((f = fopen (temp1, "wb")) == NULL)
{
return;
}
fwrite (&hist, (int) sizeof (HISTORY), 1, f);
fclose (f);
return;
}
int time_to_next ()
{
int cur_day;
int cur_hour;
int cur_minute;
int cur_mday;
int cur_mon;
int cur_year;
int junk;
int our_time;
int i;
int time_to;
int guess;
int nmin;
/* Get the current time in minutes */
dostime (&cur_hour, &cur_minute, &junk, &junk);
our_time = cur_hour * 60 + cur_minute;
/* Get the current day of the week */
dosdate (&cur_mon, &cur_mday, &cur_year, &cur_day);
cur_day = 1 << cur_day;
/* A ridiculous number */
time_to = 3000;
/* Go through the events from top to bottom */
for (i = 0; i < num_events; i++)
{
/* If it is the current event, skip it */
if (cur_event == i)
continue;
/* If it is a BBS event, skip it */
if (e_ptrs[i]->behavior & MAT_BBS)
continue;
/* If it was already run today, skip it */
if (e_ptrs[i]->last_ran == cur_mday)
continue;
/* If it doesn't happen today, skip it */
if (!(e_ptrs[i]->days & cur_day))
continue;
/* If not this day of the month, skip it */
if ((e_ptrs[i]->day) && (e_ptrs[i]->day != (char)cur_mday))
continue;
/* If not this month of the year, skip it */
if ((e_ptrs[i]->month) && (e_ptrs[i]->month != (char)cur_mon))
continue;
/* If it is earlier than now, skip it unless it is forced */
if (e_ptrs[i]->minute < our_time)
{
if (!(e_ptrs[i]->behavior & MAT_FORCED))
{
continue;
}
/* Hmm, found a forced event that has not executed yet */
/* Give the guy 2 minutes and call it quits */
guess = 2;
}
else
{
/* Calculate how far it is from now */
guess = e_ptrs[i]->minute - our_time;
}
/* If less than closest so far, keep it */
if (time_to > guess)
time_to = guess;
}
/* If we still have nothing, then do it again, starting at midnight */
if (time_to >= 1441)
{
/* Calculate here to midnight */
nmin = 1440 - our_time;
/* Go to midnight */
our_time = 0;
/* Go to the next possible day */
cur_day = cur_day << 1;
if (cur_day > DAY_SATURDAY)
cur_day = DAY_SUNDAY;
/* Go through the events from top to bottom */
for (i = 0; i < num_events; i++)
{
/* If it is a BBS event, skip it */
if (e_ptrs[i]->behavior & MAT_BBS)
continue;
/* If it doesn't happen today, skip it */
if (!(e_ptrs[i]->days & cur_day))
continue;
/* If not this day of the month, skip it */
if ((e_ptrs[i]->day) && (e_ptrs[i]->day != (char)cur_mday))
continue;
/* If not this month of the year, skip it */
if ((e_ptrs[i]->month) && (e_ptrs[i]->month != (char)cur_mon))
continue;
/* Calculate how far it is from now */
guess = e_ptrs[i]->minute + nmin;
/* If less than closest so far, keep it */
if (time_to > guess)
time_to = guess;
}
}
if (time_to > 1440)
time_to = 1440;
if (time_to < 1)
time_to = 1;
return (time_to);
}